import { defineConfig } from '@rsbuild/core';
import { pluginImageCompress } from '@rsbuild/plugin-image-compress';
import { pluginSass } from '@rsbuild/plugin-sass';
import { pluginVue } from '@rsbuild/plugin-vue';
import { pluginVueJsx } from '@rsbuild/plugin-vue-jsx';
import { presetAttributify } from '@unocss/preset-attributify';
import { presetWind3 } from '@unocss/preset-wind3';
import { UnoCSSRspackPlugin } from '@unocss/webpack/rspack';
import AutoImport from 'unplugin-auto-import/rspack';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers';
import Components from 'unplugin-vue-components/rspack';

// const target = `${process.env.PUBLIC_PROXY}`;
const port = Number.parseInt(`${process.env.PUBLIC_PORT}` as string, 10);
const base = `${process.env.PUBLIC_BASE_URL}`;

export default defineConfig({
  plugins: [
    pluginVue(),
    pluginVueJsx(),
    pluginImageCompress(),
    pluginSass({
      sassLoaderOptions: {
        sourceMap: true,
        additionalData: `@use "@/styles/element/index.scss" as *;`,
      },
    }),
  ],
  tools: {
    rspack: {
      plugins: [
        AutoImport({
          include: [
            /\.[tj]sx?$/, // .ts, .tsx, .js, .jsx
            /\.vue$/,
            /\.vue\?vue/, // .vue
            /\.vue\.[tj]sx?\?vue/, // .vue (vue-loader with experimentalInlineMatchResource enabled)
            /\.md$/, // .md
          ],
          imports: ['vue'],
          defaultExportByFilename: false,
          dirs: ['src/composable/*', 'src/constants/*'],
          dts: 'src/dts/auto-imports.d.ts',
          ignoreDts: ['ignoredFunction', /^ignore_/],
          vueTemplate: false,
          resolvers: [
            ElementPlusResolver({
              importStyle: 'sass',
            }),
          ],
          viteOptimizeDeps: true,
          injectAtEnd: true,
          biomelintrc: {
            enabled: false,
            filepath: './.biomelintrc-auto-import.json',
          },
        }),
        Components({
          resolvers: [
            ElementPlusResolver({
              importStyle: 'sass',
            }),
          ],
          include: [/\.vue$/, /\.vue\?vue/, /\.vue\.[tj]sx?\?vue/],
          dts: 'src/dts/components.d.ts',
          deep: true,
          dirs: ['src/components', 'src/views'],
        }),
        UnoCSSRspackPlugin({
          presets: [presetWind3(), presetAttributify()],
        }),
      ],
    },
  },
  server: {
    base,
    port,
    // proxy: {
    //   '^/ui/(cpoe|sde|ca|rfs|pps|mips|test|mpi|uum|apm|mdm|rvp|das|pts|oai|bms|mqa|pds|urp|cim|isms|rcs|pay|qms|disd)':
    //     {
    //       target,
    //       changeOrigin: false,
    //     },
    //   '/apm-bff-server': {
    //     target,
    //     changeOrigin: false,
    //   },
    //   '/serverTime': {
    //     target,
    //     changeOrigin: false,
    //   },
    //   '/browser/perfData': {
    //     target: 'http://192.168.20.43:12800',
    //   },
    //   '/browser/errorLogs': {
    //     target: 'http://192.168.20.43:12800',
    //   },
    //   '/v3/segments': {
    //     target: 'http://192.168.20.43:12800',
    //   },
    //   '/login': {
    //     target,
    //     changeOrigin: false,
    //   },
    //   '/logout': {
    //     target,
    //     changeOrigin: false,
    //   },
    //   '/oauth2': {
    //     target,
    //     changeOrigin: false,
    //   },
    //   '/cas': {
    //     target,
    //     changeOrigin: false,
    //   },
    // },
  },
  resolve: {
    alias: {
      '@': './src',
    },
  },
  source: {
    entry: { index: './src/main.ts' },
  },
  html: {
    template: './public/index.html',
  },
});
